package LintCode;

import java.util.Stack;

/**
 * @Classname LongestABSubstring
 * @Description TODO
 * @Date 2021/9/7 14:00
 * @Created by ASUS
 */
public class LongestABSubstring {
    public int getAns(String S) {
        // Write your code here
        if (S == null || S.length() <= 1) return 0;
        StringBuilder sb = new StringBuilder(S);
        int end = S.length() - 1;
        while (end > 0) {
            for (int i = end; i < S.length(); i ++) {
                String substring = sb.substring(i - end, i + 1);
                if (judge(substring)) return substring.length();
            }
            end--;
        }
        return 0;
    }

    public boolean judge(String str) {
        if (str == null || (str.length() & 1) == 1) return false;
        int a = 0;
        for (char c : str.toCharArray()) {
            if (c == 'A') a++;
        }
        return 2 * a == str.length();
    }

    public static void main(String[] args) {
        System.out.println(new LongestABSubstring().getAns("ABBABBABA"));
    }
}
